//
//  WeiBoPictureView.m
//  微博
//
//  Created by zxy on 2018/12/24.
//  Copyright © 2018 zxy. All rights reserved.
//

#import "WeiBoPictureView.h"

@interface WeiBoPictureView()
@end
@implementation WeiBoPictureView




-(void)setModel:(StatuesModel *)model{
    _model = model;
    for (UIView *sub in self.subviews) {
        [sub removeFromSuperview];
    }
    for (NSInteger i = 0; i < model.pic_urls.count; i++) {
        NSDictionary *dic = [model.pic_urls objectAtIndex:i];
        CGSize size = CGSizeZero;
        NSString *thumbnail_picUrl = dic[@"thumbnail_pic"];
        if(model.pic_urls.count == 1){//单张图片，直接返回计算大小就是图片本身大小,需要减去底部间距
            size = CGSizeMake(model.calcPicSize.width, model.calcPicSize.height - picMargin);
        }
        else if(model.pic_urls.count == 4){//田字格
            maxColum = 2;
            size = CGSizeMake(picWidth, picWidth);
        }
        else{
            maxColum = 3;
            size = CGSizeMake(picWidth, picWidth);
        }
        NSInteger row = i % maxColum;
        NSInteger column = i / maxColum;
        NSInteger x = row*(size.height + picMargin);
        NSInteger y = column*(size.width + picMargin) + picMargin;

        UIImageView *picImgView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y,size.width, size.height)];
        picImgView.tag = i;
        [picImgView sd_setImageWithURL:[NSURL URLWithString:thumbnail_picUrl]];
        picImgView.contentMode = UIViewContentModeScaleAspectFill;
        picImgView.clipsToBounds = YES;
        [self addSubview:picImgView];
        [picImgView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(pictureClick:)]];
        picImgView.userInteractionEnabled = YES;
    }
}

#pragma mark -配图视图点击事件
-(void)pictureClick:(UITapGestureRecognizer *)tap{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[XMGPictureClickIndexKey] = @(tap.view.tag);
    dict[XMGPictureClickModelKey] = self.model;
    [[NSNotificationCenter defaultCenter] postNotificationName:XMGPictureClickIndexNoticationName object:nil userInfo:dict];
}

@end
